package com.zsinfo.guoss.supplier.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

import com.zsinfo.guoss.supplier.MyApplication;


/**
 * Created by wkh on 2018/6/5.
 */
public class NetBroadcastReceiver extends BroadcastReceiver {

    private NetEvent netEvent;

    @Override
    public void onReceive(Context context, Intent intent) {

        ConnectivityManager connectivityManager=(ConnectivityManager) MyApplication.instance.getSystemService(context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        if(networkInfo!=null&&networkInfo.isAvailable()) {
            // 接口回调传过去状态的类型
            if (netEvent != null){
                netEvent.onNetChange(1);
                Log.e("NetBroadcastReceiver", "当前网络正常");
            } else {
                Log.e("NetBroadcastReceiver", "null");
            }
        } else {
            if (netEvent != null){
                netEvent.onNetChange(0);
                Log.e("NetBroadcastReceiver", "当前网络处于断网");
            } else {
                Log.e("NetBroadcastReceiver", "null");
            }
        }
    }

    public void setNetEvent(NetEvent event){
       this.netEvent=event;
    }
}
